var app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,
    isCustomer:false,
    isMallSaleMan:false,
    isNOCustomer:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setTabBarStyle({
    //   color: '#87827A',
    //   selectedColor: '#CB9B4C',
    //   backgroundColor: '#232224',
    //   borderStyle: 'white'
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    console.log(app.globalData)
    if (app.globalData.customerId!=""){
      
      //用户信息
      wx.request({
        url: app.getUrl() + '/api/customer/getCustomer',
        data:{
          customerId:app.globalData.customerId,
        },
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        success(res) {
          if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
            //非公司客户只能看到VIP资讯和我的麦沁
            if(res.data.obj.integral==0){
              that.setData({
                isNOCustomer:true
              })
            }else{
              that.setData({
                isCustomer:true,
              })
            }
            
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail(res) {
          console.log(res)
          wx.showToast({
            title: '连接服务器失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }else{
      this.setData({
        isCustomer:false,
      })
    }
    if (app.globalData.customerId!="" || Object.keys(app.globalData.mallSaleMan).length!=0){
      this.setData({
        isLogin:true,
      })
    }else{
      this.setData({
        isLogin:false,
      })
    }
    if(Object.keys(app.globalData.mallSaleMan).length==0){
      this.setData({
        isMallSaleMan: false,
      })
    }else{
      this.setData({
        isMallSaleMan: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  select(e){
    if (this.data.isLogin==false){
      wx.navigateTo({
        url: '../login/login',
      })
    }else{
      wx.navigateTo({
        url: '../' + e.currentTarget.dataset.info + '/' + e.currentTarget.dataset.info,
      })
    }
  },
  loginin(){
    wx.navigateTo({
      url: '../login/login',
    })
  },
  loginout(){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '确认退出登录吗？',
      success(res) {
        if (res.confirm) {
          if(app.globalData.customerId){
            wx.request({
              url: app.getUrl() +'/api/customer/logout',
              data:{
                Authorization: app.globalData.customerId + ":" + app.globalData.accessToken
              },
              method: 'POST',
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              success(res){
                console.log(res)
                if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
                  $Toast({
                    content: '退出成功',
                    type: 'success',
                    duration: 2,
                  });
                  app.globalData.customerId = "";
                  app.globalData.accessToken = "";
                  app.globalData.openId = "";
                  that.setData({
                    isLogin:false,
                    isCustomer:false,
                    isNOCustomer:false,
                  })
                  try {
                    wx.setStorageSync('customerId', "")
                    wx.setStorageSync('accessToken', "")
                    wx.setStorageSync('openId', "")
                  } catch (e) {
                    wx.setStorageSync('customerId', "")
                    wx.setStorageSync('accessToken', "")
                    wx.setStorageSync('openId', "")
                  }
                  that.onShow()
                } else {
                  if (res.data.error_code == 403 || res.data.status==403){
                    // wx.showModal({
                    //   title: '提示',
                    //   content: '该账户已在别处登录，是否跳转至登录页面？',
                    //   success(res) {
                    //     if (res.confirm) {
                    //       wx.redirectTo({
                    //         url: '../login/login',
                    //       })
                    //     } else { }
                    //   }
                    // })
                    $Toast({
                      content: '退出成功',
                      type: 'success',
                      duration: 2,
                    });
                    app.globalData.customerId = "";
                    app.globalData.accessToken = "";
                    app.globalData.openId = "";
                    that.setData({
                      isLogin:false,
                      isCustomer:false,
                      isNOCustomer:false
                    })
                    try {
                      wx.setStorageSync('customerId', "")
                      wx.setStorageSync('accessToken', "")
                      wx.setStorageSync('openId', "")
                    } catch (e) {
                      wx.setStorageSync('customerId', "")
                      wx.setStorageSync('accessToken', "")
                      wx.setStorageSync('openId', "")
                    }
                    that.onShow()
                  }else{
                    $Toast({
                      content: res.data.msg,
                      type: 'warning',
                      duration: 2,
                    });
                  } 
                }
              },
              fail(res) {
                console.log(res)
                $Toast({
                  content: '连接服务器失败',
                  type: 'error',
                  duration: 2,
                });
              }
            })
          }
          if(app.globalData.mallSaleMan){
            app.globalData.mallSaleMan = {};
            that.setData({
              isLogin:false,
              isMallSaleMan:false,
            })
            try {
              wx.setStorageSync('mallSaleMan', {})
            } catch (e) {
              wx.setStorageSync('mallSaleMan', {})
            }
            $Toast({
              content: '退出成功',
              type: 'success',
              duration: 2,
            });
          }
        } else{}
      }
    })
    
  }
})